<?php

function register($username,$password,$email,$other = array())
{
	if(!empty($GLOBALS['_CFG']['shop_reg_closed']))
	{
		$GLOBALS['err']->add($GLOBALS['_LANG']['shop_register_closed']);
	}
	
	if(empty($username))
	{
		$GLOBALS['err']->add($GLOBALS['_LANG']['username_empty']);
	}
	else 
	{
		if(preg_match('/\'\/^\\s*$|^c:\\\\con\\\\con$|[%,\\*\\"\\s\\t\\<\\>\\&\'\\\\]/', $username))
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['username_invalid'],htmlspecialchars($username)));
		}
	}
	
	if(empty($email))
	{
		$GLOBALS['err']->add($GLOBALS['_LANG']['email_empty']);
	}
	else 
	{
		if(!is_email($email))
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['email_invalid'],htmlspecialchars($email)));
		}
	}
	
	if($GLOBALS['err']->error_no >0)
	{
		return false;
	}
	
	if(!$GLOBALS['user']->add_user($username,$password,$email))
	{
		if($GLOBALS['user']->error == ERR_INVALID_USERNAME)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['username_invalid'],$username));
		}
		elseif($GLOBALS['user']->error == ERR_USERNAME_NOT_ALLOW)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['username_not_allow'],$username));
		}
		elseif($GLOBALS['user']->error == ERR_USERNAME_EXISTS)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['username_exists'],$username));
		}
		elseif($GLOBALS['user']->error == ERR_INVALID_EMAIL)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['email_invalid'],$email));
		}
		elseif($GLOBALS['user']->error == ERR_EMAIL_NOT_ALLOW)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['email_not_allow'],$email));
		}
		elseif($GLOBALS['user']->error == ERR_EMAIL_EXISTS)
		{
			$GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['email_exist'],$email));
		}
		else 
		{
			$GLOBALS['err']->add('UNKNOWN ERROR!');
		}
		
		return false;
	}
	else 
	{
		$GLOBALS['user']->set_session($username);
		$GLOBALS['user']->set_cookie($username);
		
		return true;
	}
}